<template>
  <div class="financial-balance">
    <vertical-table :page="pageIndex + '/' + pageCount" class="table" :label-width="215" :labels="labels" :coldata="colData" @lastpage="handleLastPage" @nextpage="handleNextPage"></vertical-table>
  </div>
</template>

<script>
import { requestAjax } from '../../../common.js'
import CONSTANT from '../../../constant/constant.js'

export default {
  data() {
    return {
      labels: [
        {
          name: '财务报表指标',
          label: '',
          color: '#F8B200',
          dark: true,
          bold: true
        },
        {
          name: '营业收入',
          label: 'revenue'
        },
        {
          name: '营业利润',
          label: 'operationProfit'
        },
        {
          name: '净利润',
          label: 'netProfit'
        },
        {
          name: '货币资金',
          label: 'bankCash'
        },
        {
          name: '总资产',
          label: 'totalAssets'
        },
        {
          name: '总负债',
          label: 'totalLiability'
        },
        {
          name: '净资产',
          label: 'totalNetAssets'
        },
        {
          name: '经营活动产生的净现金流',
          label: 'runNetFlows'
        },
        {
          name: '盈利能力指标',
          label: '',
          color: '#F8B200',
          dark: true,
          bold: true
        },
        {
          name: '毛利率',
          label: 'grossMargin'
        },
        {
          name: '净利率',
          label: 'netMargin'
        },
        {
          name: '成长能力指标',
          label: '',
          color: '#F8B200',
          dark: true,
          bold: true
        },
        {
          name: '营业收入增长率',
          label: 'operationIncomeRate'
        },
        {
          name: '净利润增长率',
          label: 'netProfitRate'
        },
        {
          name: '净资产增长率',
          label: 'totalNetAssetsRate'
        },
        {
          name: '运营能力指标',
          color: '#F8B200',
          dark: true,
          bold: true,
          label: ''
        },
        {
          name: '应收账款周转率',
          label: 'accountReceivableTurnoverRate'
        },
        {
          name: '存货周转率',
          label: 'inventoryTurnoverRate'
        },
        {
          name: '营运周期',
          label: 'operatingCycle'
        },
        {
          name: '流动资金',
          label: 'currentCash'
        },
        {
          name: '管理费用率',
          label: 'managerChargeRate'
        },
        {
          name: '销售费用率',
          label: 'saleChargeRate'
        },
        {
          name: '偿债能力指标',
          dark: true,
          bold: true,
          color: '#F8B200',
          label: ''
        },
        {
          name: '资产负债率',
          label: 'financeBalanceRate'
        },
        {
          name: '流动比率',
          label: 'flowRate'
        },
        {
          name: '速动比率',
          label: 'fastFlowRate'
        }
      ],
      colData: [ {}, {}, {} ],
      pageCount: 1,
      pageIndex: 1
    }
  },
  created() {
    this.id = this.$route.query.id || ''
    this.load()
  },
  methods: {
    load(...args) {
      if (this.id) {
        this.loadDataById(...args)
      } else {
        this.loadData(...args)
      } 
    },
    loadData(index = 1) {
      const url = CONSTANT.URL.SYSTEM.FINANCIALINDEX
      const params = {
        pageIndex: index,
        pageSize: 3,
        associationId:this.$router.currentRoute.query.userId || 0
      }
      requestAjax(url, '{}', params, res => {
        this.pageCount = res.data.pageCount
        this.pageIndex = index
        if (!res.data.bussData) return
        this.colData = res.data.bussData.map(i => {
          return {
            header: {
              text: i.rptDate
            },
            data: {
              ...i,
              isCheck: i.isCheck == 'y' ? '是' : '否'
            }
          }
        })
        while(this.colData.length < 3) {
          this.colData.push([{header: {}, data: {}}])
        }
      })
    },
    loadDataById(index = 1) {
      const url = CONSTANT.URL.SYSTEM.FINANCIALINDEX
      const params = {
        pageIndex: index,
        pageSize: 3,
        userId: this.id
      }
      requestAjax(url, '{}', params, res => {
        this.pageCount = res.data.pageCount
        this.pageIndex = index
        if (!res.data.bussData) return
        this.colData = res.data.bussData.map(i => {
          return {
            header: {
              text: i.rptDate
            },
            data: {
              ...i,
              isCheck: i.isCheck == 'y' ? '是' : '否'
            }
          }
        })
        while(this.colData.length < 3) {
          this.colData.push([{header: {}, data: {}}])
        }
      })
    },
    handleLastPage() {
      if (this.pageIndex <= 1) {
        this.$message.warning('已经是第一页了')
      } else {
        this.loadData(this.pageIndex - 1)
      }
    },
    handleNextPage() {
      if (this.pageIndex >= this.pageCount) {
        this.$message.warning('已经是最后一页了')
      } else {
        this.loadData(this.pageIndex + 1)
      }
    }
  },
  components: {
    "vertical-table": () => import("../../common/vertical-table.vue")
  }
}
</script>

<style scoped>
.table {
  margin-right: 158px;
}
</style>
